<template>
  <div class="">
    <div class="company-body">
                <span class="company-container">
                 <span v-show="false" class="company-nanme" v-text="company_left"></span>
                  <span>
                    <img v-bind:src="getIcon(company_left)"
                         class="img-circle circle-border  ">
                  </span>

                </span>
      <span class="center-container">
                 VS
                </span>
      <span class="company-container">
                  <img v-bind:src="getIcon(company_right)"
                       class="img-circle circle-border  ">
                  <span  v-show="false" class="company-nanme" v-text="company_right"></span>
                </span>
    </div>
    <div class="bar-body" v-for="(item, index) in data.data">
      <span class="bar-container">
        <c-progress :text-inside="true" :stroke-width="30" :percentage="calculationRatio(item.data,0)"
                     :show-val="item.data[0].data +''" class="left-bar" :color="calculationColor(item.data,0)"
                     :show-text="true" ></c-progress>
      </span>
      <span class="center-container indicator-container">
        <i v-text="item.indicator"></i>
        <br/>
        <i style="font-size: 8px;font-style: italic;" v-text="formatUnit(item)" ></i>
      </span>
      <span class="bar-container">
        <c-progress :text-inside="true" :stroke-width="30" :percentage="calculationRatio(item.data,1)"
                     :show-val="item.data[1].data  +''" class="tight-bar" :color="calculationColor(item.data,1)"
                     :show-text="true"></c-progress>
      </span>
    </div>
  </div>

</template>

<script>

  let ContextPath = "/js/aiw";

  //四舍五入法
  Number.prototype.toRound = function (num) {
    return Math.round(this * Math.pow(10, num)) / Math.pow(10, num);
  };

  //去尾法
  Number.prototype.toFloor = function (num) {
    return Math.floor(this * Math.pow(10, num)) / Math.pow(10, num);
  };

  //公司图标
  const COMPANY_ICON ={
    "中国平安": ContextPath + "/pingan.png",
    "中国人寿":  ContextPath +"/zhongguorenshou.jpg",
    "中国太保":  ContextPath +"/zhongguotaibao.jpg",
    "新华保险":  ContextPath +"/xinhuabaoxian.jpg",
  };

  import CProgress from "./src/progress.vue";

  export default {
    name: 'ChangeTrendChart',
    components: {
      CProgress
    },
    props: {
      company_left: {
        type: String,
        default: ''
      },
      company_right: {
        type: String,
        default: ''
      },
      data: {
        type: Object,
        default: () => ({
          data:[]
        })
      },
      showIndex:{
          type: Number,
          default: 0
      },
      // zIndex: {
      //   type: Number,
      //   default: 1
      // },
      // className: {
      //   type: String,
      //   default: ''
      // }
    },
    data() {
      return {
        testdata: [
          {
            "year":2017,
            "type":1,
            "data":[
              {
                "indicator":"保费收入",
                "unit":"元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":211093000000.1234
                  },
                  {
                    "company":"中国人寿",
                    "data":1000000
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":233
                  },
                  {
                    "company":"中国人寿",
                    "data":111
                  }
                ]
              },
              {
                "indicator":"内含价值",
                "unit":"元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":3333
                  },
                  {
                    "company":"中国人寿",
                    "data":22
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":2222
                  },
                  {
                    "company":"中国人寿",
                    "data":2000
                  }
                ]
              },
              {
                "indicator":"归母净利润",
                "unit":"元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":23
                  },
                  {
                    "company":"中国人寿",
                    "data":8
                  }
                ]
              }
            ]
          },
          {
            "year":2017,
            "type":2,
            "data":[
              {
                "indicator":"保费收入",
                "unit":"百万元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":341390
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"内含价值",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"归母净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              }
            ]
          },
          {
            "year":2017,
            "type":3,
            "data":[
              {
                "indicator":"保费收入",
                "unit":"百万元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":471772
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"内含价值",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"归母净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              }
            ]
          },
          {
            "year":2017,
            "type":4,
            "data":[
              {
                "indicator":"保费收入",
                "unit":"百万元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":605035
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"内含价值",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"归母净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              }
            ]
          },
          {
            "year":2018,
            "type":1,
            "data":[
              {
                "indicator":"保费收入",
                "unit":"百万元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":253109
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"内含价值",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"归母净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              }
            ]
          },
          {
            "year":2018,
            "type":2,
            "data":[
              {
                "indicator":"保费收入",
                "unit":"百万元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":408194
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"内含价值",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"归母净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              }
            ]
          },
          {
            "year":2018,
            "type":3,
            "data":[
              {
                "indicator":"保费收入",
                "unit":"百万元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":561835
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"内含价值",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"归母净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              }
            ]
          },
          {
            "year":2018,
            "type":4,
            "data":[
              {
                "indicator":"保费收入",
                "unit":"百万元",
                "data":[
                  {
                    "company":"中国平安",
                    "data":719556
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"内含价值",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              },
              {
                "indicator":"归母净利润",
                "unit":"",
                "data":[
                  {
                    "company":"中国平安",
                    "data":null
                  },
                  {
                    "company":"中国人寿",
                    "data":null
                  }
                ]
              }
            ]
          }
        ],
        oneData: {
          "year":2017,
          "type":1,
          "data":[
            {
              "indicator":"保费收入",
              "unit":"百万元",
              "data":[
                {
                  "company":"中国平安",
                  "data":211093.1234
                },
                {
                  "company":"中国人寿",
                  "data":null
                }
              ]
            },
            {
              "indicator":"净利润",
              "unit":"元",
              "data":[
                {
                  "company":"中国平安",
                  "data":233
                },
                {
                  "company":"中国人寿",
                  "data":111
                }
              ]
            },
            {
              "indicator":"内含价值",
              "unit":"元",
              "data":[
                {
                  "company":"中国平安",
                  "data":null
                },
                {
                  "company":"中国人寿",
                  "data":null
                }
              ]
            },
            {
              "indicator":"净利润",
              "unit":"",
              "data":[
                {
                  "company":"中国平安",
                  "data":null
                },
                {
                  "company":"中国人寿",
                  "data":null
                }
              ]
            },
            {
              "indicator":"归母净利润",
              "unit":"",
              "data":[
                {
                  "company":"中国平安",
                  "data":null
                },
                {
                  "company":"中国人寿",
                  "data":null
                }
              ]
            }
          ]
        },

      }
    },
    mounted() {
      // this.oneData = this.testdata[this.showIndex];
    },
    activated() {

    },
    destroyed() {

    },
    methods: {
      formatUnit(item){

/*        if(item.unit === "元"){
          let cp1 = item.data[0].data;
          let cp2 = item.data[1].data;
          cp1=cp1?cp1:0;
          cp2=cp2?cp2:0;
          let min = cp1>cp2?cp2:cp1;
// console.info("min>>>>>>>>",min)
          if(min > 0 && min >= 1000000 ){//最小值不为0且大于1百万则用百万作为单位
            item.unit = "百万元"

            item.data[0].data = (item.data[0].data/1000000).toFloor(4)
            item.data[1].data = (item.data[1].data/1000000).toFloor(4)
          }

        }

        // console.info("oneData>>>>>>>>",this.oneData)*/
        let unit = item.unit;
        if(unit && unit.trim().length > 0){
          unit = "(" + unit +")";
        }
        return unit;
      },

      calculationRatio(data,type){
        let ret_percentage = 0;
        let cp1 = data[0].data;
        let cp2 = data[1].data;
        let total = 0;
        cp1=cp1?cp1:0;
        cp2=cp2?cp2:0;
        total = cp1+cp2;

        if(total === 0){//两边无数据
          ret_percentage = 0;
        }else{

          if(type === 1){//右边
          let temp = cp1;
            cp1 = cp2;
            cp2 = temp;
          }

          ret_percentage = (cp1/total).toRound(2) * 100
        }

        return ret_percentage;
      },
      calculationColor(data,type){
        let ret_Color = '#C0C4CC';
        let percentage = this.calculationRatio(data,type);

        if(percentage >= 60){
          return  '#67C23A';
        }

        if(percentage >= 30){
          return  '#E6A23C';
        }

        if(percentage > 0){
          return  '#F56C6C';
        }

        return ret_Color;
      },

      getIcon(cn){
        console.info(cn)
        return COMPANY_ICON[cn];
      }

    },
    watch:{
      // oneData(){
      //
      // },
    },

  }
</script>

<style >
  .left-bar{
    transform:rotate(180deg)
  }
  .left-bar .el-progress-bar__innerText{
    transform:rotate(180deg)
  }

  .company-body > span {
    display: inline-block;
    vertical-align: middle;
    padding-bottom: 40px;
  }

  .company-nanme{
    font-size: 24px;
    font-weight: 400;
  }

  .bar-container{
    width: 30%;

  }
  .center-container{
    text-align: center;
    vertical-align: middle;
    width: 150px;
    color: #606266;
    height: 30px;
    line-height: 25px;

  }
  .indicator-container{

    border-left:  1px solid #e8e8e8;
    border-right:  1px solid #e8e8e8;
  }

  .bar-body{
    /*width: 80%;*/

  }
  .bar-body > span{
    display: inline-block;
    vertical-align: middle;
    padding-bottom: 50px;
  }


  .bar-body:last-child span{
    padding-bottom: 0px;
  }

  img.circle-border {
    /*border: 6px solid rgba(46, 46, 46, 0.12);*/
    border-radius: 50%;
  }

  .company-body img {
    width: 120px;
    height: 120px;
    max-height:120px ;
    max-width:120px ;
    vertical-align: middle;
    float: none;
    /*object-fit: cover;*/
    object-fit: contain;
    cursor: default;
  }
  .center-container i{
    font-style: normal;
  }
</style>
